package com.futvan.z.framework.core;

import com.futvan.z.framework.util.TomcatUtil;
import com.futvan.z.system.zuser.z_user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.LiteDeviceResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

public class SuperAction extends SuperZ{

	@Autowired
	protected HttpServletRequest request;
	
	/**
	 * 获取WebApp路径
	 * @return Path
	 * 获取WebApp目录下的图片
	 * 例：String filePath = getWebAppPath()+"img\\zlogin.png";
	 * 
	 */
	protected String getWebAppPath() {
		return request.getSession().getServletContext().getRealPath(""); 
	}
	
	/**
	 * 获取文件保存目录
	 * @return
	 */
	public String getFileSavePath() {
		if(z.isNotNull(z.sp.get("FileSavePath"))) {
			return z.sp.get("FileSavePath");
		}else {
			return request.getServletContext().getRealPath("/")+"files";
		}
	}

	/**
	 * 获取运行时根目录
	 * @return
	 */
	public String getWebRootPath(){
		return request.getServletContext().getRealPath("/");
	}

	protected String CreateFileSavePath(String filepath) throws Exception{
		String fileSavePath = TomcatUtil.getTomcatProjectPath()+"/files";

		//判读路径是否为空
		if(!"".equals(filepath) && filepath!=null) {
			fileSavePath = fileSavePath+"/"+filepath;
		}

		// 判断文件目录是否存在如果不存在怎么创建
		File dir = new File(fileSavePath);
		if (!dir.exists() && !dir.isDirectory()) {
			//创建多级目录
			dir.mkdirs();
		}
		return fileSavePath;
	}
	
	/**
	 * 判读是否是手机设备访问
	 * @return
	 */
	protected boolean isMobile() {
		boolean result = false;
		if(z.isNotNull(request)) {
			LiteDeviceResolver deviceResolver = new LiteDeviceResolver();
			Device device = deviceResolver.resolveDevice(request);
			if(device.isMobile() || device.isTablet()) {
				result = true;
			}
		}
		return result;
	}
	
	/**
	 * 	从Session中获取用户ID
	 * 
	 * @return
	 */
	protected String GetSessionUserId() {
		String userId = "";
		if(request!=null) {
			Object userObj = request.getSession().getAttribute("zuser");
			if(userObj!=null && userObj instanceof z_user) {
				z_user user = (z_user) userObj;
				userId = user.getZid();
			}
		}
		return userId;
	}
	
	/**
	 * 	从Session中获取用户ID
	 * 
	 * @return
	 */
	protected z_user GetSessionUser() {
		z_user user = null;
		if(request!=null) {
			Object userObj = request.getSession().getAttribute("zuser");
			if(userObj!=null && userObj instanceof z_user) {
				user = (z_user) userObj;
			}
		}
		return user;
	}


	/**
	 * 根据用户ID获取用户
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	protected z_user getUser(String userid) throws Exception {
		z_user u = new z_user();
		if(z.isNotNull(userid)){
			z_user tempu = sqlSession.selectOne("z_user_select_zid",userid);
			if(z.isNotNull(tempu)){
				if("1".equals(tempu.getIs_start())){
					u = tempu;
				}else{
					z.Exception("获取用户信息出错|用户未启用");
				}
			}else{
				z.Exception("获取用户信息出错|userid无效");
			}
		}else{
			z.Exception("获取用户信息出错|userid is null");
		}
		return u;
	}

	
}
